package org.lc.kcjxzj.controller;

import java.util.Map;

import org.lc.kcjxzj.dto.CompareResultDTO;
import org.lc.kcjxzj.model.Kcjxzj;
import org.lc.kcjxzj.service.CompareService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/compare")
public class CompareController {
    
    private final CompareService compareService;

    public CompareController(CompareService compareService) {
        this.compareService = compareService;
    }

    @GetMapping("/form")
    public String showCompareForm() {
        return "compare-form";
    }

    @PostMapping("/new")
    public String compareNew(Kcjxzj kcjxzj, Model model) {
        // 获取比较结果
        Map<String, CompareResultDTO> results = compareService.compareWithAll(kcjxzj);

        // 将原始记录和比较结果添加到模型中
        model.addAttribute("kcjxzj", kcjxzj);
        
        // 添加各个部分的相似度和最相似记录
        if (results.containsKey("jxff")) {
            CompareResultDTO jxffResult = results.get("jxff");
            model.addAttribute("jxffSimilarity", jxffResult.getSimilarity());
            model.addAttribute("jxffMostSimilar", jxffResult.getMostSimilar());
        }

        if (results.containsKey("xcxfx")) {
            CompareResultDTO xcxfxResult = results.get("xcxfx");
            model.addAttribute("xcxfxSimilarity", xcxfxResult.getSimilarity());
            model.addAttribute("xcxfxMostSimilar", xcxfxResult.getMostSimilar());
        }

        if (results.containsKey("sjfx")) {
            CompareResultDTO sjfxResult = results.get("sjfx");
            model.addAttribute("sjfxSimilarity", sjfxResult.getSimilarity());
            model.addAttribute("sjfxMostSimilar", sjfxResult.getMostSimilar());
        }

        if (results.containsKey("cjfx")) {
            CompareResultDTO cjfxResult = results.get("cjfx");
            model.addAttribute("cjfxSimilarity", cjfxResult.getSimilarity());
            model.addAttribute("cjfxMostSimilar", cjfxResult.getMostSimilar());
        }

        if (results.containsKey("zj")) {
            CompareResultDTO zjResult = results.get("zj");
            model.addAttribute("zjSimilarity", zjResult.getSimilarity());
            model.addAttribute("zjMostSimilar", zjResult.getMostSimilar());
        }

        if (results.containsKey("yjjy")) {
            CompareResultDTO yjjyResult = results.get("yjjy");
            model.addAttribute("yjjySimilarity", yjjyResult.getSimilarity());
            model.addAttribute("yjjyMostSimilar", yjjyResult.getMostSimilar());
        }

        return "compare-result";
    }

    @PostMapping("/full")
    @ResponseBody
    public ResponseEntity<String> triggerFullCompare() {
        try {
            compareService.fullCompare();
            return ResponseEntity.ok("相似度计算完成");
        } catch (Exception e) {
            return ResponseEntity.internalServerError()
                .body("相似度计算失败：" + e.getMessage());
        }
    }
}